package com.portal.back.service.impl;

import java.util.List;

import com.portal.back.service.ISysLinkService;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;
import com.portal.domain.SysLink;
/**
 * @Title:友情链接service
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-4-6
 * 
 */
public class SysLinkService implements  ISysLinkService {
	private IBaseDao<SysLink, Integer>  dao;
	public   SysLinkService(){
		dao=new BaseDao<SysLink, Integer>(SysLink.class);
	}
	public  boolean  addLink(SysLink link){
		
		String sql="";
		try {
			if(link.getId()==null){
				sql="insert into  sys_links(name,url,logoUrl,orderNum) values(?,?,?,?)";
			   dao.insert(sql, new Object[]{link.getName(),link.getUrl(),link.getLogoUrl(),link.getOrderNum()});
			}else{
				sql="update sys_links  set name=?,url=?,logoUrl=?,orderNum=? where id=? ";
			    dao.update(sql, new Object[]{link.getName(),link.getUrl(),link.getLogoUrl(),link.getOrderNum(),link.getId()});
			}
			return  true;
		} catch (Exception e) {
			return false;
		}
		
	}
	public  List<SysLink>  getAllLink(){
		String sql="select * from sys_links ";
		List<SysLink>  list= dao.findList(sql);
		if(list!=null && list.size()>0)return list;
		return null;
	}
	  public  boolean   deleteById(String idString){
		  
		  boolean isFlag = true;
			if (idString == null || "".equals(idString))
				return false;
			String sql = "delete from sys_links where  id=?";
			String[] delteid = idString.split("_");
			try{
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from sys_links where  id="+delteid[0];
				dao.delete(sql);
			}
			}catch (Exception e) {
				e.printStackTrace();
				return  false;
			}
			return isFlag;
	  }
	  public  SysLink   getById(String id){
		  String  sql="select * from sys_links where  id=?";
		    List<SysLink> list=dao.findList(sql, new Object[]{id});
		    if(list!=null &&  list.size()>0){
		    	return list.get(0);
		    }
		    return  null;
	  }
}
